#include "m_mutex.h"
#include "inner/mutex_for_pico.h"
#include "m_memory.h"


Int32 MMutexCreate(MMutex *self)
{
    if (self == NULL)
    {
        return kErrCodeParamsErr;
    }

    memset(self, 0x00, sizeof(MMutex));

    mutex_init(&self->mutex);

    self->is_init = True;

    return kErrCodeSucc;
}

Int32 MMutexDestory(MMutex *self)
{
    if (self == NULL)
    {
        return kErrCodeParamsErr;
    }

    if (self->is_init)
    {
        memset(self, 0x00, sizeof(MMutex));

        self->is_init = False;
    }

    return kErrCodeSucc;
}

Bool MMutexEnter(MMutex *self)
{
    if (self != NULL && self->is_init)
    {
        mutex_enter_blocking(&self->mutex);
        return True;
    }

    return False;
}

void MMutexLeave(MMutex *self)
{
    if (self != NULL && self->is_init)
    {
        mutex_exit(&self->mutex);
    }
}
